package cn.cheegon.blog.controller;

import cn.cheegon.blog.common.api.CommonPage;
import cn.cheegon.blog.common.api.CommonResult;
import cn.cheegon.blog.entity.dto.PmsTagDto;
import cn.cheegon.blog.entity.req.PmsTagReq;
import cn.cheegon.blog.model.PmsTag;
import cn.cheegon.blog.service.PmsTagService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author cheegon
 * @since 2024-06-21
 */
@RestController
@RequestMapping("/admin/tag/*")
public class AdminTagController extends AdminController{

    @Autowired
    private PmsTagService pmsTagService;

    @GetMapping("detail/{id}")
    public CommonResult<PmsTagDto> getItem(@PathVariable Long id) {
        return CommonResult.success(pmsTagService.getItem(id));
    }

    @GetMapping("list")
    public CommonResult<CommonPage<PmsTagDto>> getTagList(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                          @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize) {
        return CommonResult.success(pmsTagService.getList(pageNum, pageSize));
    }

    @GetMapping("topList")
    public CommonResult<List<PmsTagDto>> getTopTagList(@RequestParam(name = "size", defaultValue = "10") Integer size) {
        return CommonResult.success(pmsTagService.getTopList(size));
    }

    @GetMapping("allList")
    public CommonResult<List<PmsTagDto>> getAllTagList() {
        return CommonResult.success(pmsTagService.getAllList());
    }
    @PostMapping("edit")
    public CommonResult edit(@RequestBody PmsTagReq req) {
        checkAdmin();
        pmsTagService.edit(BeanUtil.toBean(req, PmsTag.class));
        return CommonResult.success();
    }

    @PostMapping("add")
    public CommonResult add(@RequestBody PmsTagReq req) {
        checkAdmin();
        pmsTagService.add(BeanUtil.toBean(req, PmsTag.class));
        return CommonResult.success();
    }
    @GetMapping("del/{id}")
    public CommonResult del(@PathVariable Long id) {
        checkAdmin();
        pmsTagService.del(id);
        return CommonResult.success();
    }
}
